package com.pignest.hotel.ui.selfhelp;

import java.util.Date;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Card;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Memberchargehistrory;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbCloseAccounts;
import com.pignest.hotel.beans.User;
import com.pignest.hotel.definition.ChargeOpType;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.LivingState;
import com.pignest.hotel.definition.PayMethod;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.system.Hotel;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.system.Logic;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class HourSelfCheck extends BaseSelfCheckOutVbox {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3069741148141861833L;

	private Label used;//已消费金额
	private Label balance;//余额
	private Label payed;//已支付
    
	private Label realTime;//实际入住时长
	private Label accoutTime;//结算时长
	private Label account;//当前结算钟点金额
	public HourSelfCheck()
	{
		addEventListener("onCreate", this);
	}
	public void onEvent(Event event) throws Exception {
		super.onEvent(event);
		if ("onCreate".equals(event.getName()))
		{
			
			realTime=(Label)getFellow("realTime");
			accoutTime=(Label)getFellow("accoutTime");
			used =(Label)getFellow("used");
			balance=(Label)getFellow("balance");
			payed=(Label)getFellow("payed");
			account=(Label)getFellow("account");
			hourInit();
		}
	}
	public void hourInit(){
		Living l =super.getCurLiving();
		int iUsed=Services.getIncomeService().countAllConsumsUsed(l, true,null);//总消费
		int iStorage =Services.getIncomeService().countAllStroages(l, false);//总存
		int iPayed =Services.getIncomeService().countPayed(l,true);//已支付的
		
		int c =Logic.countUnionHours(new Date(),l.getLiveTime());
		int iAccount =c*l.getAdjustPrice();
		iUsed =iUsed +iAccount;
		int sLast=iStorage-iUsed+iPayed;//余额
		account.setValue(Utils.toPrice(iAccount));
		used.setValue(Utils.toPrice(iUsed));
		payed.setValue(Utils.toPrice(iPayed));
		balance.setValue(Utils.toPrice(sLast));
		realTime.setValue(Logic.getBetween(new Date(),l.getLiveTime() ));
		accoutTime.setValue(String.valueOf(c));
	
	}
	public String selfCheckOut(Card card) {
		try {
			Living l =super.getCurLiving();
			boolean isCheckOut = Services.getReceptionService().isCheckOut(l);
			if (isCheckOut)
				return "已结账";
			int deposit = Services.getIncomeService().countAllDeposit(l, false);
			if (deposit != 0) {
				return "抱歉你在前台有押金不能在自助上办理结账退房，请到前台办理!";
			}
			StringBuffer msg = new StringBuffer();
			Date liveTime = l.getLiveTime();
			String sCurTime = DateUtil
					.currentDateToString(DateUtil.DEFAULT_DATETIME_FORMAT);
			String sLiveTime = DateUtil.toString(liveTime,
					DateUtil.DEFAULT_DATETIME_FORMAT);
	

			int comsum = Services.getIncomeService().countAllConsumsUsed(l,
					true,null);// 除加收外的消费
			int price = l.getAdjustPrice();

			int allPrice = 0;
			int iAccount=Utils.toPrice(account.getValue());
			String sAccoutTime=accoutTime.getValue();
			msg.append("你好:").append(l.getCustormName()).append("\n");
			msg.append("入住时间").append(sLiveTime).append("\n");
			msg.append("结账时间：").append(sCurTime).append("\n");
			TbAccounts ac = null;
			if (iAccount != 0) {
				
				allPrice = comsum + iAccount;
				msg.append("当前总消费为:").append(Utils.toPrice(allPrice)).append("\n");
				msg.append("其中结算时长:").append(sAccoutTime).append("\n");
				msg.append("结算钟点金额为：").append(Utils.toPrice(iAccount)).append("\n");
				ac = new TbAccounts();
				ac.setLiving(l);
				ac.setType(InComeItemType.FF.name());
				ac.setCounts(1);
				ac.setPrice(-iAccount);
				ac.setPrice2(price);
				ac.setName(InComeItemType.FF.getName());
				ac.setEndstate("是");
				ac.setEndtime(new Date());
				ac.setOpdate(DateUtil.toString(new Date(), "yyyy-MM-dd"));
				ac.setOptime(new Date());
				ac.setOpUser(l.getMember().getName());
				ac.setRemark("结算时长为" + sAccoutTime + "共计:" + Utils.toPrice(iAccount));

			} else {
				allPrice = comsum;
			}
			msg.append("当前总消费为:").append(Utils.toPrice(allPrice)).append("\n");
			if (UIUtils.confirm(msg.toString())) {
				// 当会员卡不够消费
				int balance = Services.getMemberService().queryMemberBalance(l.getMember());
				int lost = balance - allPrice;
				if (lost <= 0) {
					UIUtils.alert("会员余额不足" + Utils.toPrice(lost) + "！请及时充值！谢谢");
				}
				// 设置离开时间
				l.setLeaveDate(new Date());
				l.setLiveState(LivingState.CHECKOUT.name());
				// 会员加收时增加记录
				Memberchargehistrory mch = null;
				if (iAccount != 0) {
					mch = new Memberchargehistrory();
					mch.setMember(l.getMember());
					mch.setChargeTime(new Date());
					mch.setPrice(-iAccount);
					mch.setChargetype("自助");
					mch.setOptype(ChargeOpType.OnConsume.name());
					mch.setRemark(DateUtil.curDateStr()
							+ ChargeOpType.OnCharge.getValue()
							+ Utils.toPrice(iAccount));
				}
				TbCloseAccounts ca = new TbCloseAccounts();
				ca.setLiving(l);
				ca.setInvoice(0);
				ca.setTime(new Date());
				ca.setAccounts(allPrice);
				ca.setType(PayMethod.MEMBER_CARD.name());
				ca.setOrderNo(DateUtil.toString(l.getLiveTime(), "yyyyMMddHHmmss"));
				ca.setCheckOutNo(l.getMember().getCardNo());
				ca.setSerialNumber(Global.getLivineSerialNumber());
				User user = Hotel.getCurrentUser();
				ca.setUsername(user.getName());
				ca.setUserId(user.getId());
				l.setTbColoseAccounts(ca);
				Services.getReceptionService().updateSelfCheckOut(l, ac, ca, mch, card);
				return null;
			} else
				return "已取消";
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage();
		}
	}

}
